package com.sixth.stock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sixth.core.entity.Producer;
import com.sixth.core.feign.UserFeign;
import com.sixth.core.util.WebUtil;
import com.sixth.core.vo.R;
import com.sixth.stock.service.ProducerService;
import com.sixth.stock.vo.ProducerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
@Api(tags = "厂家管理")
@RestController
@RequestMapping("stock/producter")
public class ProducerController {
    @Resource
    private ProducerService producerService;
    @Resource
    private UserFeign userFeign;
    //分页查询，根据条件查询，模糊查询，排序，分页，多条件查询。初始化查询
    @PostMapping("/list")
    //swagger注解，@RequestBody表示接收一个json格式的参数，@ApiParam表示swagger中显示的参数名
    @ApiOperation(value = "分页查询",notes = "根据条件查询")
    public R<IPage<Producer>> list(@RequestBody ProducerVo producerVo){
        return producerService.listByPage( producerVo);
    }
    //查询所有可用的商家，用于下拉框，即status=0
    @ApiOperation(value = "查询所有可用的商家",notes = "用于下拉框")
    @GetMapping("/listAll")
    public R listAll(){
        return producerService.listAll();
    }
    //批量删除,即修改状态status
    @ApiOperation(value = "批量删除",notes = "批量删除")
    @PostMapping("/batchDelete")
    public R batchDelete(@RequestBody Long[] userIds){
        System.out.println("批量删除"+ Arrays.toString(userIds));
        return producerService.batchDelete(userIds);
    }
    //根据id删除 ,即修改状态status    PathVariable方式传参，前后端参数名可不一致，但类型必须一致，斜杠后为参数名，多个斜杠参数名之间用“,”隔开
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除",notes = "根据id删除")
    public R delete(@PathVariable Long id){
        return producerService.deleteById(id);
    }
    //使用时，前后端参数需对应
//    @PostMapping("/delete")
//    public R delete(Long id){
//        return producerService.deleteById(id);
//    }
    //新增
    @ApiOperation(value = "新增",notes = "新增")
    @PostMapping("/add")
    //@Transactional
    public R add(@RequestBody Producer producer){
        producer.setCreateTime( new Date());
        producer.setCreateBy(userFeign.getByPhone(WebUtil.getUsername()).getData().getUserName());
        return producerService.add(producer);
    }
    //修改
    @PostMapping ("/update")
    @ApiOperation(value = "修改",notes = "修改")
    //@Transactional
    public R update(@RequestBody Producer producer){
        producer.setUpdateTime(new Date());
        producer.setUpdateBy(userFeign.getByPhone(WebUtil.getUsername()).getData().getUserName());
        return producerService.update(producer);
    }
    //根据id查询,用于回显
    @ApiOperation(value = "根据id查询",notes = "根据id查询")
    @GetMapping("/{id}")
    public R<Producer> getById(@PathVariable Long id){
        return R.ok( producerService.getById(id));
    }
}
